{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot  as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.69765402]\n",
      " [-0.51142455]\n",
      " [ 0.35681532]]\n"
     ]
    }
   ],
   "source": [
    "#输入数据\n",
    "X = np.array([[1,3,3],\n",
    "             [1,4,3],\n",
    "             [1,1,1],\n",
    "             [1,0,2]])\n",
    "#标签\n",
    "Y = np.array([[1],\n",
    "             [1],\n",
    "             [-1],\n",
    "             [-1]])\n",
    "\n",
    "#权值初始化\n",
    "#[3,1]代表3个输入1个输出  \n",
    "#原本w取值在0-1之间 -0.5将取值范围变成-0.5 - 0.5之间  *2将取值范围变成-1 - 1之间\n",
    "W = (np.random.random([3,1]) - 0.5)*2 \n",
    "\n",
    "print(W)\n",
    "#学习率\n",
    "lr = 0.11\n",
    "#神经网络输出\n",
    "O = 0\n",
    "\n",
    "def update():\n",
    "    global X,Y,W,lr\n",
    "    O = np.sign(np.dot(X,W))\n",
    "    W_C = lr *(X.T.dot(Y-0)) / int(X.shape[0]) #X.shape[0]代表X的行数\n",
    "    W = W +W_C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.69765402]\n",
      " [-0.34642455]\n",
      " [ 0.43931532]]\n",
      "0\n",
      "[[-0.69765402]\n",
      " [-0.18142455]\n",
      " [ 0.52181532]]\n",
      "1\n",
      "[[-0.69765402]\n",
      " [-0.01642455]\n",
      " [ 0.60431532]]\n",
      "2\n",
      "[[-0.69765402]\n",
      " [ 0.14857545]\n",
      " [ 0.68681532]]\n",
      "3\n",
      "[[-0.69765402]\n",
      " [ 0.31357545]\n",
      " [ 0.76931532]]\n",
      "4\n",
      "[[-0.69765402]\n",
      " [ 0.47857545]\n",
      " [ 0.85181532]]\n",
      "5\n",
      "[[-0.69765402]\n",
      " [ 0.64357545]\n",
      " [ 0.93431532]]\n",
      "6\n",
      "[[-0.69765402]\n",
      " [ 0.80857545]\n",
      " [ 1.01681532]]\n",
      "7\n",
      "[[-0.69765402]\n",
      " [ 0.97357545]\n",
      " [ 1.09931532]]\n",
      "8\n",
      "[[-0.69765402]\n",
      " [ 1.13857545]\n",
      " [ 1.18181532]]\n",
      "9\n",
      "[[-0.69765402]\n",
      " [ 1.30357545]\n",
      " [ 1.26431532]]\n",
      "10\n",
      "[[-0.69765402]\n",
      " [ 1.46857545]\n",
      " [ 1.34681532]]\n",
      "11\n",
      "[[-0.69765402]\n",
      " [ 1.63357545]\n",
      " [ 1.42931532]]\n",
      "12\n",
      "[[-0.69765402]\n",
      " [ 1.79857545]\n",
      " [ 1.51181532]]\n",
      "13\n",
      "[[-0.69765402]\n",
      " [ 1.96357545]\n",
      " [ 1.59431532]]\n",
      "14\n",
      "[[-0.69765402]\n",
      " [ 2.12857545]\n",
      " [ 1.67681532]]\n",
      "15\n",
      "[[-0.69765402]\n",
      " [ 2.29357545]\n",
      " [ 1.75931532]]\n",
      "16\n",
      "[[-0.69765402]\n",
      " [ 2.45857545]\n",
      " [ 1.84181532]]\n",
      "17\n",
      "[[-0.69765402]\n",
      " [ 2.62357545]\n",
      " [ 1.92431532]]\n",
      "18\n",
      "[[-0.69765402]\n",
      " [ 2.78857545]\n",
      " [ 2.00681532]]\n",
      "19\n",
      "[[-0.69765402]\n",
      " [ 2.95357545]\n",
      " [ 2.08931532]]\n",
      "20\n",
      "[[-0.69765402]\n",
      " [ 3.11857545]\n",
      " [ 2.17181532]]\n",
      "21\n",
      "[[-0.69765402]\n",
      " [ 3.28357545]\n",
      " [ 2.25431532]]\n",
      "22\n",
      "[[-0.69765402]\n",
      " [ 3.44857545]\n",
      " [ 2.33681532]]\n",
      "23\n",
      "[[-0.69765402]\n",
      " [ 3.61357545]\n",
      " [ 2.41931532]]\n",
      "24\n",
      "[[-0.69765402]\n",
      " [ 3.77857545]\n",
      " [ 2.50181532]]\n",
      "25\n",
      "[[-0.69765402]\n",
      " [ 3.94357545]\n",
      " [ 2.58431532]]\n",
      "26\n",
      "[[-0.69765402]\n",
      " [ 4.10857545]\n",
      " [ 2.66681532]]\n",
      "27\n",
      "[[-0.69765402]\n",
      " [ 4.27357545]\n",
      " [ 2.74931532]]\n",
      "28\n",
      "[[-0.69765402]\n",
      " [ 4.43857545]\n",
      " [ 2.83181532]]\n",
      "29\n",
      "[[-0.69765402]\n",
      " [ 4.60357545]\n",
      " [ 2.91431532]]\n",
      "30\n",
      "[[-0.69765402]\n",
      " [ 4.76857545]\n",
      " [ 2.99681532]]\n",
      "31\n",
      "[[-0.69765402]\n",
      " [ 4.93357545]\n",
      " [ 3.07931532]]\n",
      "32\n",
      "[[-0.69765402]\n",
      " [ 5.09857545]\n",
      " [ 3.16181532]]\n",
      "33\n",
      "[[-0.69765402]\n",
      " [ 5.26357545]\n",
      " [ 3.24431532]]\n",
      "34\n",
      "[[-0.69765402]\n",
      " [ 5.42857545]\n",
      " [ 3.32681532]]\n",
      "35\n",
      "[[-0.69765402]\n",
      " [ 5.59357545]\n",
      " [ 3.40931532]]\n",
      "36\n",
      "[[-0.69765402]\n",
      " [ 5.75857545]\n",
      " [ 3.49181532]]\n",
      "37\n",
      "[[-0.69765402]\n",
      " [ 5.92357545]\n",
      " [ 3.57431532]]\n",
      "38\n",
      "[[-0.69765402]\n",
      " [ 6.08857545]\n",
      " [ 3.65681532]]\n",
      "39\n",
      "[[-0.69765402]\n",
      " [ 6.25357545]\n",
      " [ 3.73931532]]\n",
      "40\n",
      "[[-0.69765402]\n",
      " [ 6.41857545]\n",
      " [ 3.82181532]]\n",
      "41\n",
      "[[-0.69765402]\n",
      " [ 6.58357545]\n",
      " [ 3.90431532]]\n",
      "42\n",
      "[[-0.69765402]\n",
      " [ 6.74857545]\n",
      " [ 3.98681532]]\n",
      "43\n",
      "[[-0.69765402]\n",
      " [ 6.91357545]\n",
      " [ 4.06931532]]\n",
      "44\n",
      "[[-0.69765402]\n",
      " [ 7.07857545]\n",
      " [ 4.15181532]]\n",
      "45\n",
      "[[-0.69765402]\n",
      " [ 7.24357545]\n",
      " [ 4.23431532]]\n",
      "46\n",
      "[[-0.69765402]\n",
      " [ 7.40857545]\n",
      " [ 4.31681532]]\n",
      "47\n",
      "[[-0.69765402]\n",
      " [ 7.57357545]\n",
      " [ 4.39931532]]\n",
      "48\n",
      "[[-0.69765402]\n",
      " [ 7.73857545]\n",
      " [ 4.48181532]]\n",
      "49\n",
      "[[-0.69765402]\n",
      " [ 7.90357545]\n",
      " [ 4.56431532]]\n",
      "50\n",
      "[[-0.69765402]\n",
      " [ 8.06857545]\n",
      " [ 4.64681532]]\n",
      "51\n",
      "[[-0.69765402]\n",
      " [ 8.23357545]\n",
      " [ 4.72931532]]\n",
      "52\n",
      "[[-0.69765402]\n",
      " [ 8.39857545]\n",
      " [ 4.81181532]]\n",
      "53\n",
      "[[-0.69765402]\n",
      " [ 8.56357545]\n",
      " [ 4.89431532]]\n",
      "54\n",
      "[[-0.69765402]\n",
      " [ 8.72857545]\n",
      " [ 4.97681532]]\n",
      "55\n",
      "[[-0.69765402]\n",
      " [ 8.89357545]\n",
      " [ 5.05931532]]\n",
      "56\n",
      "[[-0.69765402]\n",
      " [ 9.05857545]\n",
      " [ 5.14181532]]\n",
      "57\n",
      "[[-0.69765402]\n",
      " [ 9.22357545]\n",
      " [ 5.22431532]]\n",
      "58\n",
      "[[-0.69765402]\n",
      " [ 9.38857545]\n",
      " [ 5.30681532]]\n",
      "59\n",
      "[[-0.69765402]\n",
      " [ 9.55357545]\n",
      " [ 5.38931532]]\n",
      "60\n",
      "[[-0.69765402]\n",
      " [ 9.71857545]\n",
      " [ 5.47181532]]\n",
      "61\n",
      "[[-0.69765402]\n",
      " [ 9.88357545]\n",
      " [ 5.55431532]]\n",
      "62\n",
      "[[ -0.69765402]\n",
      " [ 10.04857545]\n",
      " [  5.63681532]]\n",
      "63\n",
      "[[ -0.69765402]\n",
      " [ 10.21357545]\n",
      " [  5.71931532]]\n",
      "64\n",
      "[[ -0.69765402]\n",
      " [ 10.37857545]\n",
      " [  5.80181532]]\n",
      "65\n",
      "[[ -0.69765402]\n",
      " [ 10.54357545]\n",
      " [  5.88431532]]\n",
      "66\n",
      "[[ -0.69765402]\n",
      " [ 10.70857545]\n",
      " [  5.96681532]]\n",
      "67\n",
      "[[ -0.69765402]\n",
      " [ 10.87357545]\n",
      " [  6.04931532]]\n",
      "68\n",
      "[[ -0.69765402]\n",
      " [ 11.03857545]\n",
      " [  6.13181532]]\n",
      "69\n",
      "[[ -0.69765402]\n",
      " [ 11.20357545]\n",
      " [  6.21431532]]\n",
      "70\n",
      "[[ -0.69765402]\n",
      " [ 11.36857545]\n",
      " [  6.29681532]]\n",
      "71\n",
      "[[ -0.69765402]\n",
      " [ 11.53357545]\n",
      " [  6.37931532]]\n",
      "72\n",
      "[[ -0.69765402]\n",
      " [ 11.69857545]\n",
      " [  6.46181532]]\n",
      "73\n",
      "[[ -0.69765402]\n",
      " [ 11.86357545]\n",
      " [  6.54431532]]\n",
      "74\n",
      "[[ -0.69765402]\n",
      " [ 12.02857545]\n",
      " [  6.62681532]]\n",
      "75\n",
      "[[ -0.69765402]\n",
      " [ 12.19357545]\n",
      " [  6.70931532]]\n",
      "76\n",
      "[[ -0.69765402]\n",
      " [ 12.35857545]\n",
      " [  6.79181532]]\n",
      "77\n",
      "[[ -0.69765402]\n",
      " [ 12.52357545]\n",
      " [  6.87431532]]\n",
      "78\n",
      "[[ -0.69765402]\n",
      " [ 12.68857545]\n",
      " [  6.95681532]]\n",
      "79\n",
      "[[ -0.69765402]\n",
      " [ 12.85357545]\n",
      " [  7.03931532]]\n",
      "80\n",
      "[[ -0.69765402]\n",
      " [ 13.01857545]\n",
      " [  7.12181532]]\n",
      "81\n",
      "[[ -0.69765402]\n",
      " [ 13.18357545]\n",
      " [  7.20431532]]\n",
      "82\n",
      "[[ -0.69765402]\n",
      " [ 13.34857545]\n",
      " [  7.28681532]]\n",
      "83\n",
      "[[ -0.69765402]\n",
      " [ 13.51357545]\n",
      " [  7.36931532]]\n",
      "84\n",
      "[[ -0.69765402]\n",
      " [ 13.67857545]\n",
      " [  7.45181532]]\n",
      "85\n",
      "[[ -0.69765402]\n",
      " [ 13.84357545]\n",
      " [  7.53431532]]\n",
      "86\n",
      "[[ -0.69765402]\n",
      " [ 14.00857545]\n",
      " [  7.61681532]]\n",
      "87\n",
      "[[ -0.69765402]\n",
      " [ 14.17357545]\n",
      " [  7.69931532]]\n",
      "88\n",
      "[[ -0.69765402]\n",
      " [ 14.33857545]\n",
      " [  7.78181532]]\n",
      "89\n",
      "[[ -0.69765402]\n",
      " [ 14.50357545]\n",
      " [  7.86431532]]\n",
      "90\n",
      "[[ -0.69765402]\n",
      " [ 14.66857545]\n",
      " [  7.94681532]]\n",
      "91\n",
      "[[ -0.69765402]\n",
      " [ 14.83357545]\n",
      " [  8.02931532]]\n",
      "92\n",
      "[[ -0.69765402]\n",
      " [ 14.99857545]\n",
      " [  8.11181532]]\n",
      "93\n",
      "[[ -0.69765402]\n",
      " [ 15.16357545]\n",
      " [  8.19431532]]\n",
      "94\n",
      "[[ -0.69765402]\n",
      " [ 15.32857545]\n",
      " [  8.27681532]]\n",
      "95\n",
      "[[ -0.69765402]\n",
      " [ 15.49357545]\n",
      " [  8.35931532]]\n",
      "96\n",
      "[[ -0.69765402]\n",
      " [ 15.65857545]\n",
      " [  8.44181532]]\n",
      "97\n",
      "[[ -0.69765402]\n",
      " [ 15.82357545]\n",
      " [  8.52431532]]\n",
      "98\n",
      "[[ -0.69765402]\n",
      " [ 15.98857545]\n",
      " [  8.60681532]]\n",
      "99\n",
      "k [-1.85766452]\n",
      "d [ 0.08105832]\n"
     ]
    },
    {
     "data": {
      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pl9LXZNLWVm+wBfAvYvXQI3y8xGx66rDDsA08xsDuFcwkPuPjlyTdVke+Cx\n0jmRx4F73X1K5JrKNZaQKjCHcHXNhRv6oG6GEhHJsVRM8iIikgw1eRGRHFOTFxHJMTV5EZEcU5MX\nEckxNXkRkRxTkxcRyTE1eRGRHPt/02nkTqPQiqQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x235a0b73ba8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "for i in range(100):\n",
    "    update() #更新权值\n",
    "    print(W) #打印权值\n",
    "    print(i) #打印当前迭代次数\n",
    "    O = np.sign(np.dot(X,W)) \n",
    "    if(O == Y).all():\n",
    "        print(\"Finished\")\n",
    "        print(\"epochs\",i)\n",
    "        break\n",
    "\n",
    "#正样本\n",
    "x1 = [3,4]\n",
    "y1 = [3,3]\n",
    "\n",
    "#负样本\n",
    "x2 = [1,0]\n",
    "y2 = [1,2]\n",
    "\n",
    "#计算分界线的斜率以及截距\n",
    "k = -W[1]/W[2]\n",
    "d = -W[0]/W[2]\n",
    "\n",
    "print('k',k)\n",
    "print('d',d)\n",
    "\n",
    "xdata = (0,5)\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(xdata,xdata*k+d,'r')\n",
    "plt.scatter(x1,y1,c='b')\n",
    "plt.scatter(x2,y2,c='y'),\n",
    "plt.show()\n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
